
package JET.Frames.DataModels;

import JET.Centers.ControlCenter;
import JET.Centers.Graphics.GUICenter;
import JET.SwingUtils.TreeUtil;
import JET.UtilPanels.UPProject;
import JET.physics.interfaces.ProjectTreeNode;
import java.util.Vector;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author 057Ry
 */
public class ProjectTreeModel implements TreeModel {

    private Vector<TreeModelListener> treeModelListeners = new Vector<TreeModelListener>();

    private ControlCenter CC = ControlCenter.getInstance();

    public Object getRoot() {
        return CC.projectTreeRoot;
    }

    public Object getChild(Object parent, int index) {
        return ((ProjectTreeNode) parent).getChild(index);
    }

    public int getChildCount(Object parent) {
        return ((ProjectTreeNode) parent).getChildCount();
    }

    public boolean isLeaf(Object node) {
        return ((ProjectTreeNode) node).isLeaf();
    }

    public void valueForPathChanged(TreePath path, Object newValue) {
        ((ProjectTreeNode) path.getLastPathComponent()).resetNodeValue(newValue);
    }

    public int getIndexOfChild(Object parent, Object child) {
        return ((ProjectTreeNode) parent).getIndexOfChild(child);
    }

    public void addTreeModelListener(TreeModelListener l) {
        treeModelListeners.addElement(l);
    }

    public void removeTreeModelListener(TreeModelListener l) {
        treeModelListeners.removeElement(l);
    }

    public void fireWholeTreeChanged() {
        ProjectTreeNode node = CC.projectTreeRoot;
        TreePath path = new TreePath(new Object[]{node});

        JTree projectTree = ((UPProject) CC.GUIC.getUtilTab(GUICenter.UtilTabs.Project)).getProjectTree();
        String sceneTreeExpansionState = TreeUtil.getExpansionState(projectTree, 0);

        TreeModelEvent evt = new TreeModelEvent(this, path);
        for(TreeModelListener l : treeModelListeners)
            l.treeStructureChanged(evt);

        TreeUtil.restoreExpanstionState(projectTree, 0, sceneTreeExpansionState);
    }
    public void fireScenesTreeChanged() {
        ProjectTreeNode node = CC.projectTreeRoot;
        TreePath path = new TreePath(new Object[]{node,
                                                  CC.projectTreeRoot.getChild(0)
                                                 });

        JTree projectTree = ((UPProject) CC.GUIC.getUtilTab(GUICenter.UtilTabs.Project)).getProjectTree();
        String sceneTreeExpansionState = TreeUtil.getExpansionState(projectTree, 0);

        TreeModelEvent evt = new TreeModelEvent(this, path);
        for(TreeModelListener l : treeModelListeners)
            l.treeStructureChanged(evt);

        TreeUtil.restoreExpanstionState(projectTree, 0, sceneTreeExpansionState);
    }
}
